﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CodeCamp.Models.Sponsors;

namespace CodeCamp.Controllers
{
  public class SponsorsController : Controller
  {

    
    // GET: /Sponsors/

    public ActionResult List( string eventName )
    {
      int eventId = EventsHelper.GetEventId( eventName );
      var repository = new CodeCampDataRepository();
      var eventDisplayName = repository.GetEvent( eventId ).DisplayName;
      var eventSponsors = repository.GetSponsors( eventId );
      
      // assemble model
      var sponsors = new List<SponsorsListViewModel.Sponsor>();
      foreach ( var sponsor in eventSponsors )
      {
        var size = SponsorsHelper.GetScaledImageSize( sponsor.ImageWidth, sponsor.ImageHeight );

        sponsors.Add(
          new SponsorsListViewModel.Sponsor
          {
            SponsorId = sponsor.Id,
            Name = sponsor.Name,
            Url = sponsor.Url,
            ImageUrl = sponsor.ImageUrl,
            ImageWidth = size.Width,
            ImageHeight = size.Height
          } );
      }
      sponsors.PermuteRandomly();

      var communitySponsors = new List<SponsorsListViewModel.CommunitySponsor>();
      foreach ( var communitySponsor in repository.GetCommunitySponsors( eventId ) )
        communitySponsors.Add(
          new SponsorsListViewModel.CommunitySponsor
          {
            Name = communitySponsor.Name,
            Url = communitySponsor.Url
          } );

      var individualContributors = new List<SponsorsListViewModel.IndividualContributor>();
      foreach ( var individualContributor in repository.GetIndividualContributors( eventId ) )
        individualContributors.Add(
          new SponsorsListViewModel.IndividualContributor
          {
            Name = string.Format( "{0} {1}", individualContributor.FirstName, individualContributor.LastName )
          } );

      var model = 
        new SponsorsListViewModel 
        {
          EventDisplayName = eventDisplayName,
          Sponsors = sponsors,
          CommunitySponsors = communitySponsors,
          IndividualContributors = individualContributors
        };

      return View( model );
    }

    // GET: /Sponsors/Details/5

    public ActionResult Details( int id )
    {
      return View();
    }

    // GET: /Sponsors/Create

    public ActionResult Create()
    {
      return View();
    }

    // POST: /Sponsors/Create

    [HttpPost]
    public ActionResult Create( FormCollection collection )
    {
      try
      {
        // TODO: Add insert logic here

        return RedirectToAction( "Index" );
      }
      catch
      {
        return View();
      }
    }

    // GET: /Sponsors/Edit/5

    public ActionResult Edit( int id )
    {
      return View();
    }

    // POST: /Sponsors/Edit/5

    [HttpPost]
    public ActionResult Edit( int id, FormCollection collection )
    {
      try
      {
        // TODO: Add update logic here

        return RedirectToAction( "Index" );
      }
      catch
      {
        return View();
      }
    }
  }
}
